package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {


    /**
     * spring-data 会自动注入IStrategyCommentRepository实现类
     *
     * IStrategyCommentRepository 接口继承MongoRepository
     * 拥有了 分页,排序,crud基本功能
     *    底层实现 : AOP 操作--动态代理
     */
    @Autowired
    private IStrategyCommentRepository repository;

    //使用较为底层的贾琏欲执事, 实现mongodb操作, 这里用它来实现较为复杂操作
    @Autowired
    private MongoTemplate template;


    @Override
    public void saveOrUpdate(StrategyComment comment) {
        if(!StringUtils.hasLength(comment.getId())){
            //创建
            comment.setCreateTime(new Date());
        }
        repository.save(comment);
    }



    @Override
    public Page query(StrategyCommentQuery qo) {
        //查询条件
        Query query = new Query();
        if(qo.getDetailId() != -1){
            //攻略id条件
            query.addCriteria(Criteria.where("detailId").is(qo.getDetailId()));
        }
        //查总数
        long count = template.count(query, StrategyComment.class);

        if (count == 0){
            return Page.empty();
        }
        //select count(id) from xxx where ....
        //select * from xxx where .... order by
        //参数1:当前页, 注意, 从0开始
        //参数2:每页显示条数
        //参数3:排序规则, 参数a: 表示倒序  参数b:哪个字段
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(pageable);

        //查某一页  List
        List<StrategyComment> list = template.find(query, StrategyComment.class);
        // PageImpl  Page接口实现类
        return new PageImpl(list, pageable,count );
    }

    @Override
    public void thumbUp(String toid, Long userId) {

        //1:获取当前操作评论对象
        Optional<StrategyComment> op = repository.findById(toid);
        StrategyComment comment = op.get();
        if(comment != null){
            //2:获取评论中点赞用户id列表
            List<Long> list = comment.getThumbuplist();

            //3:判断传入userId是否存在用户id列表
            if (list.contains(userId)){
                //4:如果存在, 表示之前点赞过, 此操作为取消点赞
                comment.setThumbupnum(comment.getThumbupnum() - 1);
                list.remove(userId);   //删除用户id
            }else{
                //5:如果不存在,表示之前没有点赞过, 此操作为点赞
                comment.setThumbupnum(comment.getThumbupnum() + 1);
                list.add(userId);
            }
        }
        //更新数据
        this.saveOrUpdate(comment);
    }
}
